package com.cn.kehong.service;

import com.cn.kehong.api.IThroughRateDetailService;
import com.cn.kehong.dao.ThroughRateDetailMapper;
import com.cn.kehong.dao.ThroughRateMapper;
import com.cn.kehong.domain.ThroughRate;
import com.cn.kehong.domain.ThroughRateDetail;
import com.cn.kehong.domain.ThroughRateDetailExample;
import com.cn.kehong.domain.ThroughRateExample;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ThroughRateDetailServiceImpl implements IThroughRateDetailService {

    @Autowired
    private ThroughRateDetailMapper throughRateDetailMapper;
    @Autowired
    private ThroughRateMapper throughRateMapper;

    @Override
    public List<ThroughRateDetail> getThroughRateDetailsByRateId(String rateId) throws Exception {
        if(StringUtils.isEmpty(rateId)){
            return Lists.newArrayList();
        }
        ThroughRateDetailExample example = new ThroughRateDetailExample();
        example.createCriteria().andRateIdEqualTo(rateId).andStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("sort_index asc");
        List<ThroughRateDetail> throughRateDetails = throughRateDetailMapper.selectByExample(example);
        return throughRateDetails;
    }

    @Override
    public String addThroughRateDetails(List<ThroughRateDetail> throughRateDetails) throws Exception {
        if(CollectionUtils.isEmpty(throughRateDetails)){
            return "直通率为空！";
        }
        int count = throughRateDetailMapper.batchInsertThroughRateDetail(throughRateDetails);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateThroughRateDetails(List<ThroughRateDetail> throughRateDetails) throws Exception {
        if(CollectionUtils.isEmpty(throughRateDetails)){
            return "直通率为空！";
        }
        String rateId = throughRateDetails.get(0).getRateId();
        if(StringUtils.isEmpty(rateId)){
            return "缺少主键Id";
        }
        ThroughRateDetailExample example = new ThroughRateDetailExample();
        example.createCriteria().andRateIdEqualTo(rateId);

        int count = throughRateDetailMapper.deleteByExample(example);
        if(count < 0){
            return "清理直通率异常!";
        }
        count = throughRateDetailMapper.batchInsertThroughRateDetail(throughRateDetails);
        if (count < 1){
            throw new Exception("更新直通率异常！");
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public List<ThroughRateDetail> getThroughRateDetailsByKb(QueryForm queryForm) throws Exception {
        ThroughRateExample rateExample = new ThroughRateExample();
        rateExample.createCriteria().andFactoryNameEqualTo(queryForm.getFactoryName()).andThroughTimeEqualTo(queryForm.getThroughTime());
        List<ThroughRate> throughRates = throughRateMapper.selectByExample(rateExample);
        if (throughRates.size()>0){
            ThroughRateDetailExample example = new ThroughRateDetailExample();
            example.createCriteria().andRateIdEqualTo(throughRates.get(0).getId()).andStatusEqualTo(Constants.NORMAL_STATUS);
            example.setOrderByClause("sort_index asc");
            List<ThroughRateDetail> throughRateDetails = throughRateDetailMapper.selectByExample(example);
            return throughRateDetails;
        }else {
            return null;
        }

    }
}
